package com.service.impl;

import com.dao.ScoreMapper;
import com.model.Score;
import com.model.Student;
import com.model.dto.ScoreDTO;
import com.service.ScoreService;
import com.utils.PageParam;
import com.utils.Pager;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("scoreService")
public class ScoreServiceImpl implements ScoreService {

    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public Pager<ScoreDTO> getScorePage(String clazzCode, String studentName, PageParam pageParam) {
        Pager<ScoreDTO> pager = new Pager<>(0, new ArrayList<>());
        String search = "";
        if (StringUtils.isNotEmpty(studentName)) {
            search = "%" + studentName + "%";
        }
        List<String> clazzCodes = null;
        if (StringUtils.isNotEmpty(clazzCode)) {
            clazzCodes = Arrays.asList(clazzCode.split(","));
        }
        int totalCount = scoreMapper.findScoreCount(clazzCodes, search);
        if (totalCount > 0) {
            List<ScoreDTO> list = scoreMapper.findScores(clazzCodes, search, pageParam);
            pager.setTotalCount(totalCount);
            pager.setList(list);
        }
        return pager;
    }

    @Override
    public void updateScore(Score score) {
        scoreMapper.updateScore(score);
    }

    @Override
    public void saveScore(Score score) {
        scoreMapper.saveScore(score);
    }

    @Override
    public void delScore(List<Long> ids) {
        scoreMapper.delScore(ids);
    }

    @Override
    public List<ScoreDTO> getUserScores(Long id) {
        return scoreMapper.findUserScores(id);
    }
}
